#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import bs4
import requests
import furl

from fake_useragent import UserAgent


def get(url, headers={}, fake=None):
    """Get respond from url
    
    Arguments:
        url {str} -- url
    
    Keyword Arguments:
        headers {dict} -- header info (default: {{}})
        fake {None | str} -- fake user agent (default: {None})
    
    Returns:
        [type] -- [description]
    """
    if fake:
        ua = UserAgent()
        headers.update({'User-Agent': getattr(ua, fake)})
    return requests.get(url, headers)


menu = {'学习新思想': 'cc72a0454287bdedb7e2c156db55e818/71eb7214c6c0c1f5e6ec6e29564decb4.html',
'十九大时间': 'f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html',
'学习理论': 'd184e7597cc0da16f5d9f182907f1200/9a3668c13f6e303932b5e0e100fc248b.html',
'红色中国': '482f31e90c481f2161a19ecfcbb91f8b/e6109f421e472b4399b006ba21bc2ecd.html',
'学习科学': '896bddc5f57a423b857a85eb40f98945/72742e3e40c96ade71e42b6e7ed42419.html',
'环球视野': '261c9a142ef8e6375ed554815a26d585/f2d8ff735982530b7a8c9bb90fa99f68.html',
'习近平文汇': '5c90534c80d14c060d6683fa960e3676/82573c005c024095037d2186a02244cb.html',
'学习电视台': '0809b8b6ab8a81a4f55ce9cbefa16eff/ae60b027cb83715fd0eeb7bb2527e88b.html',
'学习慕课': 'f547c0f321ac9a0a95154a21485a29d6/1cdd8ef7bfc3919650206590533c3d2a.html',
'学习文化': 'b069a45c80585e78b558c64306bfdf92/53194398355c363c5c635c74a70c7b45.html',
'强军兴军': 'a9d25ec4ca996c6100d2982d33a1d8b2/4f02a12dd5663651422f5c55e10ef19e.html',
'美丽中国': 'b51582609167f915bcd0f5532daee48d/778eae63fed91ff4c54dc15f22c7fc31.html'}

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait

options = Options()
#options.headless=True
driver = webdriver.Chrome(options=options)

class xxqg:
    home = furl.furl(r'https://www.xuexi.cn')

    @staticmethod
    def login(driver):
        driver.get(xxqg.home.url)

        wait = WebDriverWait(driver, 20)
        wait.until(expected_conditions.element_to_be_clickable((By.CLASS_NAME, 'icon')))
        login = driver.find_element_by_class_name('login-icon')
        login.click()
        n = driver.window_handles
        driver.switch_to.window(n[1])
        wait.until(expected_conditions.visibility_of_element_located((By.CLASS_NAME, 'ddloginbox')))
        driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="ddlogin-iframe"]'))
        source = driver.find_element_by_css_selector('#qrcode > img')
        source.screenshot('qr.png')
        driver.switch_to.window(n[0])


    @staticmethod
    def find(topic='习近平文汇'):
        try:
            wait = WebDriverWait(driver, 20)
            wait.until(expected_conditions.visibility_of_element_located((By.CLASS_NAME, 'menu-list')))
            # soup = bs4.BeautifulSoup(driver.page_source, 'lxml')
            # menu = {a.text:a['href'] for a in soup.find('div', {'class':'menu-list'}).find_all('a')}
            driver.get((xxqg.home / menu[topic]).url)
            driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="www_xuexi_cn_cn"]'))
            soup = bs4.BeautifulSoup(driver.page_source, 'lxml')
            # print(soup)
            # for a in soup.find('div', {'class':'grid-gr'}).find_all('div', {'class':'grid-cell'}):
            #     print(a.text)
            driver.find_element_by_partial_link_text('001').click()
            n = driver.window_handles
            driver.switch_to.window(n[1])
            driver.find_element_by_partial_link_text('001').click()
            n = driver.window_handles
            driver.switch_to.window(n[1])


        finally:
            driver.close()


xxqg.login(driver)
xxqg.find()
